persistence.setNamespace("peiwan_weixin");
document.getElementById('myPhotoFile').onchange = function(evt) {
	if (!window.FileReader)
		return;
	var files = evt.target.files;
	for (var i = 0, f; f = files[i]; i++) {
		if (!f.type.match('image.*')) {
			continue;
		}
		var reader = new FileReader();
		reader.onload = (function(theFile) {
			return function(e) {
				document.getElementById('myPhotoImg').src = e.target.result;
			};
		})(f);
		reader.readAsDataURL(f);
	}
};

document.getElementById('GameIdFile').onchange = function(evt) {
	if (!window.FileReader)
		return;
	var files = evt.target.files;
	for (var i = 0, f; f = files[i]; i++) {
		if (!f.type.match('image.*')) {
			continue;
		}
		var reader = new FileReader();
		reader.onload = (function(theFile) {
			return function(e) {
				document.getElementById('GameIdImg').src = e.target.result;
			};
		})(f);
		reader.readAsDataURL(f);
	}
};

var peiwanWeixin = angular.module('peiwan_weixin', []);
peiwanWeixin
		.controller(
				'PeiwanFormCtrl',
				[
						"$scope",
						function(s) {
							var actionUploadMyPhoto = 0;
							var actionUploadGameId = 1;
							var myPhotoUrl, gameIdUrl;
							s.myPhotoImgClicked = function() {
								$("#myPhotoFile").click();
							}
							s.GameIdImgClicked = function() {
								$("#GameIdFile").click();
							}
							document.addEventListener('uploaded', function(e) {

							});
							var uploadCount = 0;
							var isOnOk = false;
							s.ok = function() {
								if (isOnOk) {
									return;
								}
								var phone = $("#phone").val();
								var gameName = $("#game").val();
								var gameNickname = $("#gameNickname").val();
								var address = $("#address").val();
								if (phone == ""
										|| gameName == ""
										|| gameNickname == ""
										|| address == ""
										|| $('#myPhotoImg').attr("src")
												.indexOf("add2.png") != -1
										|| $('#GameIdImg').attr("src").indexOf(
												"add2.png") != -1) {
									swal({
										title : "",
										text : "请填写完整表单",
										type : "error"
									});
									return;
								}
								$("#load_modal").show();
								isOnOk = !isOnOk;
								uploadCount = 0;
								uploadFile(new Date().getTime() + "_"
										+ randomStr(128), "#myPhotoFile",
										actionUploadMyPhoto);
								uploadFile(new Date().getTime() + "_"
										+ randomStr(128), "#GameIdFile",
										actionUploadGameId);
							}
							var afterUpload = function() {
								uploadCount++;
								if (uploadCount == 2) {
									var phone = $("#phone").val();
									var gameName = $("#game").val();
									var gameNickname = $("#gameNickname").val();
									var address = $("#address").val();
									ajaxWeixin(
											"users/createPeiwanForm",
											{
												session : persistence
														.get("session"),
												phone : phone,
												gameName : gameName,
												gameNickname : gameNickname,
												headPhoto : myPhotoUrl,
												idPhoto : gameIdUrl,
												address : address
											},
											function(data) {
												$("#load_modal").css("display",
														"none");
												var json = JSON.parse(data);
												if (json.status == 0) {
													swal(
															{
																title : "",
																text : "申请提交成功",
																type : "success"
															},
															function() {
																location.href = "index.html";
															});
												} else if (json.status == 1) {
													swal(
															{
																title : "",
																text : "登录过期",
																type : "error"
															},
															function() {
																location.href = "index.html";
															});
												}
											});
								}
							}
							document.addEventListener("uploaded", function(e) {
								var url = settings.getImgLinkUrl()
										+ e.detail.path;
								var action = e.detail.action;
								switch (action) {
								case actionUploadMyPhoto:
									myPhotoUrl = url;
									afterUpload();
									break;
								case actionUploadGameId:
									gameIdUrl = url;
									afterUpload();
									break;
								}
							});
						} ]);